package com.ruoyi.project.dict.chengyutag.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.dict.chengyutag.domain.ChengyuTag;
import com.ruoyi.project.dict.chengyutag.mapper.ChengyuTagMapper;
import com.ruoyi.project.dict.chengyutag.service.IChengyuTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 成语标签Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-12-01
 */
@Service
@DataSource(DataSourceType.DICT)
public class ChengyuTagServiceImpl implements IChengyuTagService {

    @Autowired
    private ChengyuTagMapper chengyuTagMapper;

    /**
     * 查询成语标签
     *
     * @param tagId 成语标签ID
     * @return 成语标签
     */
    @Override
    public ChengyuTag selectChengyuTagById(Long tagId) {
        return chengyuTagMapper.selectChengyuTagById(tagId);
    }

    /**
     * 查询成语标签列表
     *
     * @param chengyuTag 成语标签
     * @return 成语标签
     */
    @Override
    public List<ChengyuTag> selectChengyuTagList(ChengyuTag chengyuTag) {
        return chengyuTagMapper.selectChengyuTagList(chengyuTag);
    }

    /**
     * 新增成语标签
     *
     * @param chengyuTag 成语标签
     * @return 结果
     */
    @Override
    public int insertChengyuTag(ChengyuTag chengyuTag) {
        chengyuTag.setCreateTime(DateUtils.getNowDate());
        return chengyuTagMapper.insertChengyuTag(chengyuTag);
    }

    /**
     * 修改成语标签
     *
     * @param chengyuTag 成语标签
     * @return 结果
     */
    @Override
    public int updateChengyuTag(ChengyuTag chengyuTag) {
        chengyuTag.setUpdateTime(DateUtils.getNowDate());
        return chengyuTagMapper.updateChengyuTag(chengyuTag);
    }

    /**
     * 删除成语标签对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteChengyuTagByIds(String ids) {
        return chengyuTagMapper.deleteChengyuTagByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除成语标签信息
     *
     * @param tagId 成语标签ID
     * @return 结果
     */
    @Override
    public int deleteChengyuTagById(Long tagId) {
        return chengyuTagMapper.deleteChengyuTagById(tagId);
    }

    /**
     * 查询成语标签树列表
     *
     * @return 所有成语标签信息
     */
    @Override
    public List<Ztree> selectChengyuTagTree() {
        List<ChengyuTag> chengyuTagList = chengyuTagMapper.selectChengyuTagList(new ChengyuTag());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ChengyuTag chengyuTag : chengyuTagList) {
            Ztree ztree = new Ztree();
            ztree.setId(chengyuTag.getTagId());
            ztree.setpId(chengyuTag.getParentTagId());
            ztree.setName(chengyuTag.getName());
            ztree.setTitle(chengyuTag.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
